<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetLuckyList.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\duobao;

use app\api\model\dao\DuoBao;
use app\api\model\dao\Goods;
use app\api\model\dao\IssuesNumber;
use app\api\model\dao\Lucky;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;
use app\library\Flag;

class GetLuckyList extends PageBase
{

    private $objLucky;
    private $objIssuesNumber;
    private $objGoods;
    private $objDuoBao;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();
        $this->objLucky = new Lucky();
        $this->objIssuesNumber = new IssuesNumber();
        $this->objGoods = new Goods();
        $this->objDuoBao = new DuoBao();
        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'goods_id' => 'require',
        ];
        $msg = [
            'goods_id.require' => '商品ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $luckys = [];
        $user_duobao = ['0' => 0];
        $users = [];
        $user_account = [];

        $arrConds = [
            'goods_id' => $param['goods_id'],
        ];
        $arrFields = [];
        $counts = $this->objLucky->getCount($arrConds);
        $rets = $this->objLucky->getLuckyByCond($arrConds, $arrFields, $offset, $limit, 'db_no desc');
        if (!empty($rets)) {
            $luckys = array_map(function ($v) {
                return $v['issues_no'];
            }, $rets);
            $arrConds = [
                'issues_no' => ['in', implode(',', $luckys)],
            ];
            $arrFields = [];
            $duobaos = $this->objDuoBao->getDuoBaoByCond($arrConds, $arrFields);
            if (!empty($duobaos)) {
                foreach ($duobaos as $duobao) {
                    if (!isset($user_duobao[$duobao['issues_no']])) {
                        $user_duobao[$duobao['issues_no']] = 1;
                    } else {
                        $user_duobao[$duobao['issues_no']] += 1;
                    }
                }
            }

            foreach ($rets as $ret) {
                $users[] = $ret['user_id'];
            }
            $arrConds = [
                'id' => ['in', implode(',', $users)],
            ];
            $arrFields = ['id', 'user_name'];
            $users = $this->objUserAccount->getUserAccountByConds($arrConds, $arrFields);
            if (!empty($users)) {
                foreach ($users as &$user) {
                    $user_account[$user['id']] = $user;
                }
            }

            $i = count($rets);
            foreach ($rets as &$ret) {
                $ret['no'] = "第" . $i . "期";
                $ret['reward_id'] = $ret['user_id'];
                $ret['reward_name'] = $user_account[$ret['user_id']]['user_name'];
                $ret['buy_volume'] = $user_duobao[$ret['issues_no']];;
                $ret['open_time'] = date('Y年m月d日 H时i分s秒', $ret['create_time']);
                $ret['reward_no'] = $ret['db_no'];
                unset($ret['db_no']);
                unset($ret['user_id']);
                $i--;
            }
        }

        if ($rets === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        $this->data = array(
            'list' => $rets,
            'total' => $counts,
        );

        return $this->getRes();
    }
}
